package com.xxs.model;

import java.util.Date;
import java.util.List;

import com.xxs.common.Const;
import com.xxs.ext.jfinal.Model;

import com.jfinal.kit.HashKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.ehcache.CacheKit;

/**
 */
@SuppressWarnings("serial")
public class Admin extends Model<Admin> {

    public static final Admin dao = new Admin();
    private static final String ADMIN_CACHE = "admin";
    private static final String ADMIN_PAGE_FOR_INDEX_CACHE = "adminPageForIndex";
    private static final String ADMIN_PAGE_FOR_ADMIN_CACHE = "adminPageForAdmin";
    private static final String ADMIN_LIST_CACHE = "adminList";
    
    public Admin() {
        super(ADMIN_CACHE);
    }

    /* get */
    public Admin get(int id) {
        return loadModel(id);
    }
    public Admin getByEmailAndPassword(String email, String password){
    	password = HashKit.md5(password);
        return dao.findFirst("select id, adminname, email, password from admin where email=? and password=?", email, password);
    }
    
    public List<Admin> getAdminListbyPoint(int count){
        return dao.findByCache(ADMIN_LIST_CACHE, 1, "select * from admin order by point desc limit 0,?",count);
    }
    
    public Page<Admin> getPageForAdmin(Integer pageNumber) {
    	String cacheName = ADMIN_PAGE_FOR_ADMIN_CACHE;
    	Page<Admin> adminPage = dao.paginateByCache(cacheName, pageNumber,
    			pageNumber, Const.PAGE_SIZE_FOR_ADMIN,
    			"select id", "from admin order by registDate desc");
    	return loadModelPage(adminPage);
    }
    
    public Page<Admin> getPage(int pageNumber){
        Page<Admin> adminPage = dao.paginateByCache(ADMIN_PAGE_FOR_INDEX_CACHE, pageNumber,
                pageNumber, Const.PAGE_SIZE_FOR_ADMIN,
                "select id", "from admin order by registDate desc");
        return loadModelPage(adminPage);
    }
    /* other */
    public void mySave(){
        this.filterText("adminname", "headImg");
        String password = HashKit.md5(this.getStr("password"));
        this.set("password", password).set("registDate", new Date());
        // bug,数据库默认头像没生效
        this.removeNullValueAttrs();
        this.save();
    }

    public void myUpdate() {
        this.filterText("username", "phone","email");
        this.update();
        removeThisCache(this.getInt("id"));
    }
    public boolean containEmail(String email) {
        return dao.findFirst("select email from admin where email=? limit 1", email) != null;
    }
    public boolean containAdminname(String adminname) {
        return dao.findFirst("select adminname from admin where adminname=? limit 1", adminname) != null;
    }
    public boolean containEmailExceptThis(int adminID, String email) {
        return dao.findFirst("select email from admin where email=? and id!=? limit 1", email, adminID) != null;
    }
    public boolean containAdminnameExceptThis(int adminID, String adminname) {
        return dao.findFirst("select adminname from admin where adminname=? and id!=? limit 1", adminname, adminID) != null;
    }

    /* getter */
    public boolean getIsAdmin(String email) {
        return Const.ADMIN_EMAIL.equals(email);
    }

    /* cache */
    private void removeThisCache(int id){
        CacheKit.remove(ADMIN_CACHE, id);
    }
}
